package com.doubleview;

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import org.codehaus.groovy.control.CompilationFailedException;

/**
 * @author huchengchao@kuaishou.com
 * Created on 2021-11-19
 */
public class GroovyScriptEngineExample {


    public static void main(String[] args) {
        try {
            //定义Groovy脚本引擎的根路径
            String[] roots = new String[]{
                    "/Users/huchengchao/project/doubleview/java-awesome-demo/language-samples/groovy/src/main/java"
                            + "/com/doubleview/"};
            GroovyScriptEngine engine = new GroovyScriptEngine(roots);
            Binding binding = new Binding();
            binding.setVariable("language", "Groovy");
            Object value = engine.run("com.doubleview.SimpleScript.groovy", binding);
            assert value.equals("The End");
        } catch (CompilationFailedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
